/**
 * 
 */
package org.log4jeditor.xml.editor.validation;

import static org.junit.Assert.assertEquals;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.validation.ContentValidator;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.DirtiesProjectFiles;

/**
 * @author Xi
 * 
 */
public class CategoryContentValidatorTestCase extends AbstractUITestCase {

	private ContentValidator validator;

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-validation-category-priority.xml");
		// remove all markers on document
		file.deleteMarkers(ContentValidator.DEFAULT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		file.deleteMarkers(ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		
		validator = new ContentValidator();
		validator.connect(document);
	}

	@After
	public void tearDown() throws Exception {
		validator.disconnect(document);
		super.tearDownProject();
	}

	@Test
	public void testScanAndValidateValidCategoryName() throws Exception {
		int offset = getDocumentOffset(13, 22, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		assertEquals("Wrong marker level:", IMarker.SEVERITY_WARNING, getMarkers()[0].getAttribute(IMarker.SEVERITY));
		assertEquals("Wrong marker type", ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, getMarkers()[0].getType());
	}
	
	@Test
	public void testScanAndValidateInvalidCategoryName() throws Exception {
		int offset = getDocumentOffset(17, 19, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
	}

	@Test
	public void testScanAndValidateEmptyCategoryName() throws Exception {
		int offset = getDocumentOffset(10, 19, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
	}

	@Test
	public void testScanAndValidateMissingCategoryName() throws Exception {
		int offset = getDocumentOffset(25, 12, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		assertEquals("Wrong type of marker:", ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, getMarkers()[0].getType());
	}
	
	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateNewInvalidCategoryName() throws Exception {
		int offset = getDocumentOffset(13, 20, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		assertEquals("Wrong marker type", ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, getMarkers()[0].getType());
		document.replace(offset, "test".length(), "unknownLogger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidLoggerNameWithPackageName() throws Exception {
		int offset = getDocumentOffset(17, 19, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "test");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidLoggerNameWithClassName() throws Exception {
		int offset = getDocumentOffset(17, 19, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "test.TestLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidCategoryName() throws Exception {
		int offset = getDocumentOffset(17, 21, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
		document.replace(offset, "unknowncategory".length(), "test.unknowncategory");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 2, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepValidCategoryName() throws Exception {
		int offset = getDocumentOffset(13, 19, "category");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		assertEquals("Wrong marker type", ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, getMarkers()[0].getType());
		document.replace(offset, "test.TestAppender".length(), "test.TestLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		assertEquals("Wrong marker type", ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, getMarkers()[0].getType());
	}

}
